<?php
class Zend_AccountController extends Zend_Controller_Action
{
	/*
	* Create the sign up form
	*/
	private function getSignupForm()
	{
		//Create Form
	 	$form = new Zend_Form();
	 	$form->setAction('success');
	 	$form->setMethod('post');
	 	$form->setDescription("sign up form");
	 	$form->setAttrib('sitename', 'loudbite');

	 	//Add Elements

	 	//Create Username Field.
	 	$form->addElement('text', 'username');
	 	$usernameElement = $form->getElement('username');
	 	$usernameElement->setLabel('Username:');
	 	$usernameElement->setOrder(1)->setRequired(true); // setRequired ở đây là yêu cầu không đc để trống

	 	//Create Email Field
	 	$form->addElement('text', 'email');
	 	$emailElement = $form->getElement('email');
	 	$emailElement->setLabel('Email:');
	 	$emailElement->setOrder(3)->setRequired(true);

	 	//Create Password Field.
	 	$form->addElement('password', 'password');
	 	$passwordElement = $form->getElement('password');
	 	$passwordElement->setLabel('Password:');
	 	$passwordElement->setOrder(2)->setRequired(true);

	 	$form->addElement('submit', 'submit');
	 	$submitButton = $form->getElement('submit');
	 	$submitButton->setLabel('Create My Account!');
	 	$submitButton->setOrder(4);

	 	return $form;
	}


	public function init()
	{

	}

	public function indexAction()
	{
		//action body
	}

	/*
	* Process the account form
	*/
	public function successAction()
	{
		$form = $this->getSignupForm();
		//Check if the submitted data is POST type
		if ($form->isValid($_POST)) {
			$email = $form->getValue("email");
			$username = $form->getValue("username");
			$password = $form->getValue("password");
            
            $userData = array(
                        "username" => $username,
                        "email" => $email,
                        "password" => $password
                        );
            
             Zend_Loader::loadClass('AccountModel');
  	         $model = new AccountModel();
             $model->addAccount($userData);
             
             //Send out thank you email.
             
		} else {
			$this->view->errors = $form->getMessages(); //thống báo lỗi nếu có
			$this->view->form = $form;			
		}
	}
	 /*Account Sign Up*/
	 public function newAction()
	 {
	 	//Get the form
	 	$form = $this->getSignupForm();

	 	//Add the form to the view
	 	$this->view->form = $form;
	 }

	 /*Active account*/
	 public function activeAction()
	 {
	 	//Fetch the email to update from the query param 'email'
	 	$emailToActivate = $this->_request->getQuery("email");
	 	//Check if the email exists
	 	//Activate the Account
	 }

	 /**
	 *	Update the user's data
	 */
	 public function updateAction()
	 {
	 	//Check if the user is logged in

	 	//Get the user's id

	 	//Get the user's information

	 	//create the Zend_View object
	 	$view = new Zend_View(); 
	 	//$view->setScriptPath("<ABSOLUTE PATH TO VIEW DIRECTION>"); //<-- Thay doi duong dan den file view mac dinh
	 	//$view->render("update.phtml");
	 }
     
     public function displayAction()
     {
        Zend_Loader::loadClass('AccountModel'); //* Load file ở trong thư mục Model vào
        $model = new AccountModel(); //* tạo một đối tượng
        $result = $model->getAccount();
        Zend_Debug::dump($result);//lenh thu du lieu
        $this->view->result = $result;
     }
     
}

?>